package com.example.demo.Contrller;

import com.example.demo.Service.paw;
import com.example.demo.model.RegionStats;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class DataController {

    @RequestMapping("/")
    public String show(Model model)throws IOException {
        int total = 0;
        int per = 0;
        List<RegionStats> allcases = paw.getdata();
        for (RegionStats r:allcases) {
            total += r.getLatestTotalCases();
            per += r.getDiffFromPrevDay();
        }
        model.addAttribute("allcases",allcases);
        model.addAttribute("total",total);
        model.addAttribute("per",per);
        return "show";

    }
    @RequestMapping("/search")
    public String search(HttpServletRequest request, Model   model) throws IOException{
        String country = request.getParameter("country");
        List<RegionStats> list=new ArrayList<>();
        int total=0;
        int pertotal=0;
        for (RegionStats r:paw.CoronaVirusData){
            if(r.getCountry().equals(country)){
                total+=r.getLatestTotalCases();
                pertotal+=r.getDiffFromPrevDay();
                list.add(r);
            }
        }
        model.addAttribute("total", total);
        model.addAttribute("pertotal", pertotal);
        model.addAttribute("list", list);
        return "search";
    }
}